﻿namespace Yipusoft.BaseDoc.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Yipusoft.BaseDoc.DBUtility;
    using Yipusoft.BaseDoc.Model;

    public class DocSearchKey
    {
        public bool Delete(Guid Id)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.UniqueIdentifier) };
            commandParameters[0].Value = Id;
            return (SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.StoredProcedure, "DocSearchKey_Delete", commandParameters) > 0);
        }

        public DocSearchKeyInfo GetDetail(Guid Id)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.UniqueIdentifier) };
            commandParameters[0].Value = Id;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.CONN_STRING, CommandType.StoredProcedure, "DocSearchKey_GetDetail", commandParameters))
            {
                DocSearchKeyInfo readerData = new DocSearchKeyInfo();
                if (reader.Read())
                {
                    readerData = GetReaderData(reader);
                }
                return readerData;
            }
        }

        public List<DocSearchKeyInfo> GetList(string Where)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@StrWhere", SqlDbType.VarChar, 0x1f40) };
            commandParameters[0].Value = Where;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.CONN_STRING, CommandType.StoredProcedure, "DocSearchKey_GetList", commandParameters))
            {
                List<DocSearchKeyInfo> list = new List<DocSearchKeyInfo>();
                while (reader.Read())
                {
                    list.Add(GetReaderData(reader));
                }
                return list;
            }
        }

        private static DocSearchKeyInfo GetReaderData(SqlDataReader reader)
        {
            DocSearchKeyInfo info2 = new DocSearchKeyInfo();
            info2.CreartUser = reader["CreartUser"].ToString();
            info2.CreateDate = Convert.ToDateTime(reader["CreateDate"]);
            info2.DepartmentId = reader["DepartmentId"].ToString();
            info2.Id = new Guid(reader["Id"].ToString());
            info2.KeyWord = reader["KeyWord"].ToString();
            info2.Sign = Convert.ToInt32(reader["Sign"]);
            return info2;
        }

        public Guid Insert(DocSearchKeyInfo model)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.UniqueIdentifier, 0x10), new SqlParameter("@DepartmentId", SqlDbType.VarChar, 10), new SqlParameter("@KeyWord", SqlDbType.VarChar, 50), new SqlParameter("@CreartUser", SqlDbType.VarChar, 50), new SqlParameter("@CreateDate", SqlDbType.DateTime), new SqlParameter("@Sign", SqlDbType.Int, 4) };
            commandParameters[0].Value = model.Id;
            commandParameters[1].Value = model.DepartmentId;
            commandParameters[2].Value = model.KeyWord;
            commandParameters[3].Value = model.CreartUser;
            commandParameters[4].Value = model.CreateDate;
            commandParameters[5].Value = model.Sign;
            if (SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.StoredProcedure, "DocSearchKey_Insert", commandParameters) > 0)
            {
                return new Guid(commandParameters[0].Value.ToString());
            }
            return Guid.Empty;
        }

        public bool Update(DocSearchKeyInfo model)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.UniqueIdentifier, 0x10), new SqlParameter("@DepartmentId", SqlDbType.VarChar, 10), new SqlParameter("@KeyWord", SqlDbType.VarChar, 50), new SqlParameter("@CreartUser", SqlDbType.VarChar, 50), new SqlParameter("@CreateDate", SqlDbType.DateTime), new SqlParameter("@Sign", SqlDbType.Int, 4) };
            commandParameters[0].Value = model.Id;
            commandParameters[1].Value = model.DepartmentId;
            commandParameters[2].Value = model.KeyWord;
            commandParameters[3].Value = model.CreartUser;
            commandParameters[4].Value = model.CreateDate;
            commandParameters[5].Value = model.Sign;
            return (SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.StoredProcedure, "DocSearchKey_Update", commandParameters) > 0);
        }
    }
}

